﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUL;
using Public;

namespace Presentation
{
    public partial class frmCreatePType : Form
    {
        ProductTypeBUL proType = new ProductTypeBUL();
        int selectedTypeId;
        public frmCreatePType()
        {
            InitializeComponent();
        }
        private void frmCreatePType_Load(object sender, EventArgs e)
        {
            searchProType("");
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string searchValue = txtSearch.Text.Trim();
            searchProType(searchValue);
        }

        public void searchProType(string searchValue)
        {
            dgvProType.DataSource = proType.searchProType(searchValue);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Public.Type pt = new Public.Type();
            pt.TypeName = txtProType.Text.Trim();
            proType.AddProductType(pt);
            frmCreatePType_Load(sender, e);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Public.Type pt = new Public.Type();
            pt.TypeName = txtProType.Text.Trim();
            pt.TypeId = this.selectedTypeId;
            proType.UpdateProductType(pt);
            frmCreatePType_Load(sender, e);
        }

        private void selectedItem(object sender, DataGridViewCellEventArgs e)
        {
            int index = dgvProType.CurrentRow.Index;
            string typeId = dgvProType.Rows[index].Cells[0].Value.ToString();
            this.selectedTypeId = int.Parse(typeId);
            this.txtProType.Text = dgvProType.Rows[index].Cells[1].Value.ToString();
        }

        private void frmCreatePType_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult rs = MessageBox.Show("Are you really want to exist?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (rs == DialogResult.OK)
            {
                frmCreatePName createPName = new frmCreatePName();
                this.Hide();
                createPName.Show();
            }
            else
            {
                e.Cancel = true;
            }
        }
    }
}
